package com.fluidops.datacatalog.reltables;

import java.util.Set;
import java.util.TreeSet;

import com.fluidops.datacatalog.reltables.model.Dataset;
import com.fluidops.datacatalog.reltables.model.Entity;
import com.fluidops.datacatalog.reltables.schemacompare.SchemaMatcher;
import com.fluidops.datacatalog.reltables.sim.EntitySetSimilarity;

public class EntityComplementScorer {

	public static double ALREADY_COMPUTED = -1d;
	
	private EntitySetSimilarity entitySetSimilarity;
	private SchemaMatcher schemaMatcher;
	
	public EntityComplementScorer(EntitySetSimilarity entitySetSimilarity, SchemaMatcher schemaMatcher){
		this.entitySetSimilarity = entitySetSimilarity;
		this.schemaMatcher = schemaMatcher;
	}
	
	public double compare(Dataset ds1 , Dataset ds2){
		double result = ALREADY_COMPUTED;
		
		if(!ds1.hasRelatednessScore(ds2)){

			Set<Entity> entities1 = ds1.getEntities();
			Set<Entity> entities2 = ds2.getEntities();
			
			/*
			 * consistency check - here - currently omitted
			 */
			
			
			double entityScore = this.entitySetSimilarity.calcEntitySetSimilarity(entities1, entities2);
			
			Set<String> ds1labels = createLabelSet(ds1);
			Set<String> ds2labels = createLabelSet(ds2);
			
			double schemaScore = this.schemaMatcher.compareSchemas(ds1labels, ds2labels);
			result = entityScore * schemaScore;
			
			//add related datasets
			ds1.addRelatedDataset(ds2, result);
			ds2.addRelatedDataset(ds1, result);
			
		}
		
		return result;
	}

	
	private Set<String> createLabelSet(Dataset ds){
		Set<String> dslabels = new TreeSet<String>();
		
		dslabels.addAll(ds.dimStringLabels());
//		dslabels.addAll(ds.dimSignalStringLabels());
//		dslabels.addAll(ds.dimValueStringLabels());
		dslabels.addAll(ds.dimValueSignalStringLabels());
		
		return dslabels;
	}
}
